package java学习.leetcode.editor.cn;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

/**
 * @author 刘世锦
 * 2022-11-22 23:24:29	 当前时间
 */
//给定一棵二叉树的根节点 root ，请找出该二叉树中每一层的最大值。 
//
// 
//
// 示例1： 
//
// 
//
// 
//输入: root = [1,3,2,5,3,null,9]
//输出: [1,3,9]
// 
//
// 示例2： 
//
// 
//输入: root = [1,2,3]
//输出: [1,3]
// 
//
// 
//
// 提示： 
//
// 
// 二叉树的节点个数的范围是 [0,104] 
// -231 <= Node.val <= 231 - 1 
// 
//
// 
// Related Topics 树 深度优先搜索 广度优先搜索 二叉树 
// 👍 284 👎 0

public class 在每个树行中找最大值{
	public static void main(String[] args) {
		Solution solution = new 在每个树行中找最大值().new Solution();
		
	}
//leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
	/**
	 *
	 * _   5 3 9 _
	 *
	 *  3
	 *  1 3
	 */
    public List<Integer> largestValues(TreeNode root) {
		List<Integer> list = new ArrayList<>();
		Deque<TreeNode> deque = new ArrayDeque<>();
		if (root!=null){
			deque.offer(root);
			while (!deque.isEmpty()){

				int size = deque.size();
				int max = Integer.MIN_VALUE;
				for (int i = 0; i < size; i++) {
					TreeNode node = deque.poll();
					max = Math.max(node.val,max);
					if (node.left!=null){
						deque.offer(node.left);
					}
					if (node.right!=null){
						deque.offer(node.right);
					}
				}
				list.add(max);
			}
		}
		return list;
	}
}
	public class TreeNode {
		int val;
		TreeNode left;
		TreeNode right;
		TreeNode() {}
		TreeNode(int val) { this.val = val; }
		TreeNode(int val, TreeNode left, TreeNode right) {
			this.val = val;
			this.left = left;
			this.right = right;
		}
	}
//leetcode submit region end(Prohibit modification and deletion)

}
